package com.hwua.demo03;

import java.io.*;

/**
 * @author Administrator
 */
public class Demo02 {
    public static void main(String[] args) {

        Demo02 d = new Demo02();
        try {

            /*d.writeToFile(new File("C:\\C\\C.txt"),"Hello");
            d.writeToFile(new File("D:\\D\\D.txt"),"World");*/

            String str1 = d.readFile(new File("C:\\C\\C.txt"));
            String str2 = d.readFile(new File("D:\\D\\D.txt"));

            d.writeToFile(new File("E:\\E\\E.txt"),str1+str2);

        } catch (IOException e) {
            e.printStackTrace();
        }

        /*try {
            System.out.println(d.readFile(new File("C:\\C\\C.txt"))+d.readFile(new File("D:\\D\\D.txt")));
        } catch (IOException e) {
            e.printStackTrace();
        }*/

    }

    public String readFile(File file) throws FileNotFoundException,IOException {
        if(!file.exists()){
            throw new  FileNotFoundException();
        }
        InputStream is = new FileInputStream(file);
        byte[] buffer = new byte[1024];
        int length = 0;
        StringBuilder sb = new StringBuilder();
        if(-1 != (length = is.read(buffer))){
            String str = new String(buffer,0,length,"GBK");
            sb.append(str);
        }
        return sb.toString();
    }

    public void writeToFile(File file,String content) throws IOException {
        if(!file.exists()){
            throw new FileNotFoundException();
        }
        if(content.length()==0 || content == null){
            throw new IllegalArgumentException();
        }

        OutputStream os = new FileOutputStream(file,true);

        os.write(content.getBytes());

        os.write("\r\n".getBytes());
    }
}
